//
//  ChannelPresentationCellFrameViewModel.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/13.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ChannelPresentationCellFrameViewModel.h"
#import "TAMacro.h"
#import "ChannelPostModel.h"
#import "ChannelPostContent.h"
#import "ChannelPostHotposts.h"
#import "ChannelPosts.h"
#import "ChannelPostImg.h"
#import "ChannelPostToolbar.h"
#import "ChannelPostJoin.h"
@implementation ChannelPresentationCellFrameViewModel

- (void)setChannelPost:(ChannelPostModel *)channelPost {
    _channelPost = channelPost;
    // 计算frame
    // 精选排行
    if (_channelPost.content.rankList.count != 0) {
        
        [self setUpTitleLabelFrame];
        [self setUpMoreImageFrame];
        [self setUpHeaderImageCollectionViewFrame];
        [self setUpBackViewFrame];
        [self setUpCellHeight];
        
    }
    
    
    // 精选
    if (_channelPost.content.hotPosts.posts.count != 0) {
        [self setUpHotPostTitleLabelFrame];
        [self setUpImageCollectionViewFrame];
        [self setUpAllLabelFrame];
        [self setUpHotPostBackViewFrame];
        [self setUpHotPostCellHeight];
    }

}
#pragma mark - 精选排行
// 计算标题frame
- (void)setUpTitleLabelFrame {
    CGFloat labelW = 100;
    CGFloat labelH = 30;
    CGFloat labelX = ViewMargin;
    CGFloat labelY = ViewMargin;
    _titleLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}
// 计算更多按钮frame
- (void)setUpMoreImageFrame {
    CGFloat imageW = 10;
    CGFloat imageH = 20;
    CGFloat imageX = SCREEN_WIDTH - 4 * ViewMargin;
    CGFloat imageY = ViewMargin / 2 * 3;
    _moreImageFrame = CGRectMake(imageX, imageY, imageW, imageH);
}
// 计算头像frame
- (void)setUpHeaderImageCollectionViewFrame {
    CGFloat viewW = 120;
    CGFloat viewH = 40;
    CGFloat viewX = CGRectGetMinX(_moreImageFrame) - viewW;
    CGFloat viewY = 2;
    _headerImageCollectionViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}
// 计算背景frame
- (void)setUpBackViewFrame {
    CGFloat viewX = ViewMargin;
    CGFloat viewY = ViewMargin;
    CGFloat viewW = SCREEN_WIDTH - 2 * ViewMargin;
    CGFloat viewH = CGRectGetMaxY(_titleLabelFrame) + ViewMargin;
    _backViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}

- (void)setUpCellHeight {
    _rankListCellHeight = CGRectGetMaxY(_backViewFrame) + ViewMargin;
}




#pragma mark - 精选


- (void)setUpHotPostTitleLabelFrame {
    CGFloat labelX = ViewMargin;
    CGFloat labelY = 0;
    CGFloat labelW = SCREEN_WIDTH - 4 * ViewMargin;
    CGFloat labelH = 40;
    _hotPostTitleLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}

- (void)setUpImageCollectionViewFrame {
     //除了照相类型返回6张图片以外, 全返回4张;
    if (_channelPost.content.toolbar.join.status == 3) {
        [self createSixImage];
    } else {
        [self createFourImage];
        
    }
}

- (void)createSixImage {
    
    ChannelPostImg *img0 = _channelPost.content.hotPosts.posts[0].img.firstObject;
    ChannelPostImg *img1 = _channelPost.content.hotPosts.posts[1].img.firstObject;
    ChannelPostImg *img2 = _channelPost.content.hotPosts.posts[2].img.firstObject;
    ChannelPostImg *img3 = _channelPost.content.hotPosts.posts[3].img.firstObject;
    ChannelPostImg *img4 = _channelPost.content.hotPosts.posts[4].img.firstObject;
    ChannelPostImg *img5 = _channelPost.content.hotPosts.posts[5].img.firstObject;
    CGFloat temp = img0.w / img0.h + img1.w / img1.h + img2.w / img2.h;
    CGFloat heightOfFirstLine = (SCREEN_WIDTH - 44) / temp;
    CGFloat temp2 = img3.w / img3.h + img4.w / img4.h + img5.w / img5.h;
    CGFloat heightOfSecLine = (SCREEN_WIDTH - 44) / temp2;
//    
    NSArray *imgArray = @[img0, img1, img2, img3, img4, img5];
    self.itemSizeArray = [NSMutableArray array];
    for (NSInteger i = 0; i < imgArray.count; i++) {
        ChannelPostImg *img = imgArray[i];
        if (i < imgArray.count / 2) {
            CGFloat width = img.w / img.h * heightOfFirstLine;
            CGFloat height = heightOfFirstLine;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
            
        } else {
            CGFloat width = img.w / img.h * heightOfSecLine;
            CGFloat height = heightOfSecLine;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
        }
    }
    
    CGFloat viewX = ViewMargin;
    CGFloat viewY = CGRectGetMaxY(_hotPostTitleLabelFrame);
    CGFloat viewW = SCREEN_WIDTH - 40;
    CGFloat viewH = heightOfSecLine + heightOfFirstLine + 4;
    
    _imageCollectionViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
    
}


- (void)createFourImage {
    ChannelPostImg *img0 = _channelPost.content.hotPosts.posts[0].img.firstObject;
    ChannelPostImg *img1 = _channelPost.content.hotPosts.posts[1].img.firstObject;
    ChannelPostImg *img2 = _channelPost.content.hotPosts.posts[2].img.firstObject;
    ChannelPostImg *img3 = _channelPost.content.hotPosts.posts[3].img.firstObject;
    
    CGFloat temp = img0.w / img0.h + img1.w / img1.h;
    CGFloat heightOfFirstLine = (SCREEN_WIDTH - 44) / temp;
    CGFloat temp2 = img2.w / img2.h + img3.w / img3.h;
    CGFloat heightOfSecLine = (SCREEN_WIDTH - 44) / temp2;
    //
    NSArray *imgArray = @[img0, img1, img2, img3];
    self.itemSizeArray = [NSMutableArray array];
    for (NSInteger i = 0; i < imgArray.count; i++) {
        ChannelPostImg *img = imgArray[i];
        if (i < imgArray.count / 2) {
            CGFloat width = img.w / img.h * heightOfFirstLine;
            CGFloat height = heightOfFirstLine;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
            
        } else {
            CGFloat width = img.w / img.h * heightOfSecLine;
            CGFloat height = heightOfSecLine;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
        }
    }
    
    CGFloat viewX = ViewMargin;
    CGFloat viewY = CGRectGetMaxY(_hotPostTitleLabelFrame);
    CGFloat viewW = SCREEN_WIDTH - 40;
    CGFloat viewH = heightOfSecLine + heightOfFirstLine + 4;
    
    _imageCollectionViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}

- (void)setUpAllLabelFrame {
    
    CGFloat labelW = 100;
    CGFloat labelH = 30;
    CGFloat labelX = SCREEN_WIDTH / 2 - 100 / 2;
    CGFloat labelY = CGRectGetMaxY(_imageCollectionViewFrame);
    _allLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}
- (void)setUpHotPostBackViewFrame {
    CGFloat viewX = ViewMargin;
    CGFloat viewY = 0;
    CGFloat viewW = SCREEN_WIDTH - 2 * ViewMargin;
    CGFloat viewH = CGRectGetMaxY(_allLabelFrame) + ViewMargin;
    _hotPostBackViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}

- (void)setUpHotPostCellHeight {
    _hotPostCellHeight = CGRectGetMaxY(_hotPostBackViewFrame) + ViewMargin;
}

@end
